package com.tengroup.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tengroup.entity.UserInfoEntity;
import com.tengroup.service.PasswordResetService;
import com.tengroup.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
//
@RestController
@RequestMapping("/Wmp")
public class WmpController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private PasswordResetService passwordResetService;
    //删除用户
    @DeleteMapping("/{id}")
    public boolean DeleteUser(@PathVariable Long id) {
       return userService.removeById(id);
    }

    //新增用户
    @PostMapping({"/adduser"})
    public boolean addUser(@RequestBody UserInfoEntity user) {
        return userService.addUser(user);
    }

    //查询用户
    @GetMapping("/{id}")
    public UserInfoEntity UserEdit(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }

    //更改用户
    @RequestMapping("/updateUser")
    public boolean UpdateUser(@RequestBody UserInfoEntity user) {
        return userService.updateUser(user);
    }
    //注册
   @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        String password = map.get("password");
        try{
            userService.registerUser(username,password);
            return ResponseEntity.ok("User registered successfully");
        } catch (Exception e) {
           return ResponseEntity.badRequest().body("Error during registration: " + e.getMessage());

        }
}
     //登录
     @PostMapping("/login")
     public ResponseEntity<String> login(@RequestBody Map<String,String> logindata)  throws Exception {
            String username = logindata.get("username");
            String password = logindata.get("password");
            if(userService.login(username,password)){
                return ResponseEntity.ok("User logged in successfully");
            }else {
                return ResponseEntity.status(401).body("Invalid username1 or password");
            }

        }
        @GetMapping("/findall")
    public List<UserInfoEntity> findAll() {
            return userService.list(null);
        }

        @GetMapping("/query")
    public ResponseEntity<Object> queryUsers(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Long id,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize){
        try {
            UserInfoEntity user = new UserInfoEntity();
            user.setUserName(username) ;
            user.setUserId(id);
            IPage<UserInfoEntity> result=userService.Query(user,pageNo,pageSize);
            return ResponseEntity.ok(result);

        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error during query: " + e.getMessage());
        }
        }
        //发送验证码
        @PostMapping("/sendVerificationCode")
        public String sendVerificationCode(@RequestParam String email) {
            boolean result = passwordResetService.sendVerificationCode(email);

            if (result) {
                return "验证码已发送到邮箱 " + email;
            } else {
                return "发送验证码失败，请稍后再试。";
            }
        }
    // 重置密码
    @PostMapping("/resetPassword")
    public String resetPassword(@RequestParam String email, @RequestParam String code, @RequestParam String newPassword) throws NoSuchAlgorithmException {
        System.out.println("Received email: " + email);  // 打印收到的 email 参数
        boolean result = passwordResetService.resetPassword(email, code, newPassword);
        if (result) {
            return "密码重置成功";
        } else {
            return "验证码错误或已过期，密码重置失败";
        }
    }





}
